﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;

namespace XiaoGang.Soft.XGWMS.UI
{
    static class Program
    {
        [DllImport("User32.dll")]//呼出Win32 API関数
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]//呼出Win32 API関数
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        private const int WS_SHOWNORMAL = 1;

        [STAThread]
        static void Main()
        {
            try
            {
                if (!ReadProperties())
                {
                    Application.Exit();
                    return;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteClientErrLog4Net(ex);
                Method.MessageShow("W18888","");//Program have been running.
                Application.Exit();
                return;
            }

            Process instance = RunningInstance();
            if (instance != null)
            {
                Method.MessageShow("888888","");
                HandleRunningInstance(instance);
                return;
            }
            else
            {
                bool canCreateThread;
                Mutex mutex = new Mutex(true, Application.UserAppDataPath.Replace("\\", "_"), out canCreateThread);

                if (!canCreateThread)
                {
                    MessageBox.Show("应用程序已在运行中！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                GlobalVal.SplashObj = SplashObject.GetSplash();
                Application.Run(new MainForm());
                //Application.Run(new M002_USER_GROUP());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool ReadProperties()
        {
            GlobalVal.gloDataTableMessage = Method.GetMsgDataTable();
            if (GlobalVal.gloDataTableMessage.Rows.Count < 1)
            {
                Method.MessageShow("","");
                return false;
            }

            //GlobalVal.glostrIniFilePath = System.Configuration.ConfigurationManager.AppSettings["PropertiesPath"];
            //if (GlobalVal.glostrIniFilePath.Trim().Length < 1)
            //{
            //    Method.MessageShow("");
            //    return false;
            //}
            GlobalVal.glostrMsgTitle = System.Configuration.ConfigurationManager.AppSettings["MessageBoxCaption"];
            //取得配置文件中设置的数据库名称
            GlobalVal.glostrDataBaseName = System.Configuration.ConfigurationManager.AppSettings["DataBaseName"];

            //取得打印机设置
            GlobalVal.globlPrint = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["Print"]);

            //取得图片上传大小限制
            GlobalVal.gloImageMaxSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["ImageMaxSize"]);

            //GlobalVal.glostrMsgTitle = Method.ReadFileContext("MessageTitle", "MessageBoxCaption");
            //if (GlobalVal.glostrMsgTitle.Trim().Length < 1)
            //{
            //    Method.MessageShow("");
            //    return false;
            //}
            GlobalVal.glostrSystemName = System.Configuration.ConfigurationManager.AppSettings["SystemName"];

            GlobalVal.gloSelectedGoods = new SelectedGoods();
            //GlobalVal.glostrSystemName = Method.ReadFileContext("SystemName", "SystemName");
            //if (GlobalVal.glostrSystemName.Trim().Length < 1)
            //{
            //    Method.MessageShow("");
            //    return false;
            //}
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        return process;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instance"></param>
        public static void HandleRunningInstance(Process instance)
        {
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
            SetForegroundWindow(instance.MainWindowHandle);
        }
    }
}